package com.example.suze.libraryinformationmanagementsystem.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.API;
import com.example.suze.libraryinformationmanagementsystem.net.JSONCallBack;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.ReturnStateEntity;
import com.person.CommonUtils;
import com.person.dao.BookDao;
import com.person.dao.DBManager;
import com.person.dao.NotesDao;
import com.person.entity.Book;
import com.person.entity.Notes;

import java.util.List;

/**
 * Created by suze on 2017/6/13.
 */

public class ReturnActivity extends BaseActivity{
    private EditText id;
    private String id1;
    private Button sure;
    private int status;
    private JSONCallBack callBack,callBack2;
    API api = API.getAPI(ReturnActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return);
        id = (EditText) findViewById(R.id.ev_return_book_id);
        sure = (Button) findViewById(R.id.bt_return_book_sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                id1 = id.getText().toString();
                if(id1.equals("")){
                    Toast.makeText(ReturnActivity.this,"请输入需要还的书的ID",Toast.LENGTH_SHORT).show();
                }else {
                    callBack = new JSONCallBack<ReturnStateEntity>(new ReturnStateEntity()) {
                        @Override
                        public void onSuccess(ReturnStateEntity entity) {
                            Log.v("login", entity.getState()+" "+entity.getCode());
                            if(entity.getState().equals("success") && entity.getCode() == 0){


//                                Looper.prepare();
//                                            Toast.makeText(ReturnActivity.this, "还书成功", Toast.LENGTH_LONG).show();
//                                            finish();
//                                            Looper.loop();

                                callBack2 = new JSONCallBack<ReturnStateEntity>(new ReturnStateEntity()) {
                                    @Override
                                    public void onSuccess(ReturnStateEntity entity) {
                                        if(entity.getState().equals("success") && entity.getCode() == 0){
                                            Looper.prepare();
                                            Toast.makeText(ReturnActivity.this, "还书成功", Toast.LENGTH_LONG).show();
                                            finish();
                                            Looper.loop();

                                        }else{
                                            Looper.prepare();
                                            Toast.makeText(ReturnActivity.this, "还书失败", Toast.LENGTH_LONG).show();
                                            Looper.loop();
                                        }
                                    }
                                };
                                api.sqlOporate("update", "UPDATE borrow_information set status = 0 WHERE No = "+"'"+id1+"'", callBack2);

                            }else{
                                Looper.prepare();
                                Toast.makeText(ReturnActivity.this, "还书失败", Toast.LENGTH_LONG).show();
                                Looper.loop();
                            }
                        }
                    };
                    api.sqlOporate("update", "UPDATE book set is_lend = 0 WHERE No = "+"'"+id1+"'", callBack);

//                    DBManager dbManager = new DBManager(ReturnActivity.this);
//                    BookDao bookDao = dbManager.getDaoSession().getBookDao();
//                    List<Book> book = bookDao.queryBuilder().where(BookDao.Properties.Id.eq(id1)).build().list();
//                    if(book.size()>0){
//                        Long studentId = book.get(0).getBorrowerId();
//                        bookDao.deleteByKey(id1);
//                        CommonUtils commonUtils = new CommonUtils(ReturnActivity.this);
//                        book.get(0).setStatus(0);
//                        book.get(0).setBorrowerId(1l);
//                        commonUtils.insertFriend(book.get(0));
//                        NotesDao notesDao = dbManager.getDaoSession().getNotesDao();
//                        List<Notes> notes = notesDao.queryBuilder().where(NotesDao.Properties.BookId.eq(id1)).build().list();
//                        if(notes.size()>0){
//                            for(int i = 0 ;i<notes.size();i++)
//                            {
//                                status = notes.get(i).getStatus();
//                                if(status==0){
//                                    long id = notes.get(i).getId();
//                                    notesDao.deleteByKey(id);
//                                    notes.get(i).setStatus(1);
//                                    commonUtils.insertNotes(notes.get(i));
//                                    finish();
//                                    break;
//                                }
//                                if(i==notes.size()-1){
//                                    Toast.makeText(ReturnActivity.this,"这本书并没有借出去~",Toast.LENGTH_SHORT).show();
//                                }
//                            }
//                        }else {
//                            Toast.makeText(ReturnActivity.this,"这本书并没有借出去~",Toast.LENGTH_SHORT).show();
//                        }
//
//                    }else {
//                        Toast.makeText(ReturnActivity.this,"没有找到这本书哦~",Toast.LENGTH_SHORT).show();
//                    }

                }
            }
        });
    }
}
